﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;

public class Skeleton : MonoBehaviour
{
    [SerializeField, FormerlySerializedAs("records")]
    private List<RecordTransform> initialPoseRecords = new List<RecordTransform>();

    [ContextMenu("ApplyInitialPose")]
    public void ApplyInitialPose()
    {
        foreach (RecordTransform transform in this.initialPoseRecords)
        {
            Transform transform2 = base.transform.Find(transform.path);
            if (transform2 != null)
            {
                transform2.localPosition = transform.localPosition;
                transform2.localEulerAngles = transform.localEulerAngles;
                transform2.localScale = transform.localScale;
            }
        }
    }

    [ContextMenu("RecordInitialPose")]
    public void RecordInitialPose()
    {
        this.initialPoseRecords.Clear();
        foreach (Transform transform in base.transform.GetComponentsInChildren<Transform>())
        {
            RecordTransform item = new RecordTransform {
                path = transform.GetPathInHierarchy("/", base.transform)
            };
            char[] separator = new char[] { '/' };
            item.path = string.Join("/", item.path.Split(separator).Skip<string>(1).ToArray<string>());
            item.localPosition = transform.localPosition;
            item.localEulerAngles = transform.localEulerAngles;
            item.localScale = transform.localScale;
            this.initialPoseRecords.Add(item);
        }
    }

    [Serializable]
    public class RecordTransform
    {
        public Vector3 localEulerAngles;
        public Vector3 localPosition;
        public Vector3 localScale;
        public string path;
    }
}

